<template>
  <el-dialog :title="title" v-model="model" width="800px" append-to-body>
    <desc-detail :data="formattedData" column="2" />

    <div v-if="isAudit" class="mt-4">
      <el-form :model="auditForm" label-width="auto">
        <el-form-item label="状态">
          <el-select v-model="auditForm.status" placeholder="请选择">
            <el-option label="审核通过" :value="2" />
            <el-option label="审核拒绝" :value="3" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
        <el-button @click="model = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted, computed } from "vue";
import { ElMessage } from "element-plus";
import DescDetail from "@/components/descDetail";
import { auditCommunityMember } from "@/api/realEstateManage";

const { proxy } = getCurrentInstance();

const props = defineProps({
  title: {
    type: String,
    default: "详情",
  },
  row: {
    type: Object,
    default: () => ({}),
  },
  // 是否为审核
  isAudit: {
    type: Boolean,
    default: false,
  },
});
const data = ref([]);
const emit = defineEmits(["submit"]);

const model = defineModel({
  default: false,
});

const auditForm = ref({
  status: 2,
});

// 处理row数据
onMounted(() => {
  data.value = Object.keys(props.row).map((key) => {
    return {
      label: key,
      value: props.row[key],
    };
  });
});

const fieldLabels = {
  realName: "姓名",
  phone: "手机号",
  type: "用户类型",
  communityName: "小区",
  region: "区域",
  smallAddress: "地址",
  floorName: "楼栋",
  unitNum: "单元",
  layer: "楼层",
  roomNum: "房号",

  state: "启用状态",
  status: "状态",
  remark: "备注",
  createBy: "创建者",
  createTime: "创建时间",
  updateBy: "更新者",
  updateTime: "更新时间",
};

const formattedData = computed(() => {
  return Object.entries(fieldLabels).map(([key, value]) => ({
    label: value,
    value: formatValue(key, props.row[key]),
  }));
});
function formatValue(key, value) {
  if (key === "certificateType") {
    const types = ["身份证", "护照", "港澳通行证", "台湾通行证"];
    return types[value] || value;
  }
  if (key === "state") {
    return value === 0 ? "禁用" : "启用";
  }
  if (key === "status") {
    const statuses = ["后台待审核", "业主待审核", "已审核", "拒绝"];
    return statuses[value] || value;
  }
  if (key === "type") {
    const types = ["业主本人", "家人(家庭成员)", "朋友", "租客"];
    return types[value] || value;
  }
  return value;
}
function handleConfirm() {
  // 处理确认逻辑
  if (props.isAudit) {
    auditCommunityMember({
      id: props.row.id,
      status: auditForm.value.status,
    }).then(({ data }) => {
      if (data) {
        ElMessage.success("操作成功");
        model.value = false;
        emit("submit");
      } else {
        ElMessage.error("操作失败");
      }
    });
  }
}
</script>
